package com.work.bookstore.controller;

import com.work.bookstore.mapper.BookMapper;
import com.work.bookstore.mapper.OrderMapper;
import com.work.bookstore.mapper.ShoppingCarMapper;
import com.work.bookstore.pojo.ShoppingCar;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author ppg007
 * @date 2021/1/1 18:03
 */
@Controller
public class ShoppingCarController {

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private BookMapper bookMapper;


    @RequestMapping(value = "/getShoppingCar",method = RequestMethod.POST)
    @ResponseBody
    public String getShoppingCar(HttpServletRequest request){
        String account=request.getParameter("account");
        return refresh(account);
    }

    @RequestMapping(value = "/updateShoppingCar",method = RequestMethod.POST)
    @ResponseBody
    public String updateShoppingCar(HttpServletRequest request){
        String account=request.getParameter("account");
        String newBookId=request.getParameter("newBookId");
        ShoppingCar shoppingCar=shoppingCarMapper.queryShoppingCarByAccount(account);
        if (shoppingCar==null){
            shoppingCarMapper.createShoppingCar(newBookId,account);
        }
        else {
            shoppingCarMapper.updateShoppingCar(shoppingCar.getBookList() + "∫" + newBookId,account);
        }
        return refresh(account);
    }
    public String refresh(String account){
        ShoppingCar shoppingCar=shoppingCarMapper.queryShoppingCarByAccount(account);
        String bookList=shoppingCar.getBookList();
        String[] temp=bookList.split("∫");
        JSONArray jsonArray=new JSONArray();
        for (String t:temp){
            jsonArray.put(BookController.putData(bookMapper.queryBookByOrder(Integer.parseInt(t))));
        }
        return jsonArray.toString();
    }

    @RequestMapping(value = "/deleteInShoppingCar",method = RequestMethod.POST)
    @ResponseBody
    public String delete(HttpServletRequest request){
        String account=request.getParameter("account");
        String bookList=request.getParameter("bookList");
        ShoppingCar shoppingCar=shoppingCarMapper.queryShoppingCarByAccount(account);
        String[] temp=bookList.split("∫");
        StringBuilder builder=new StringBuilder();
        List<String> delete = new ArrayList<>(Arrays.asList(temp));
        String[] old=shoppingCar.getBookList().split("∫");
        for (String t:old){
            if (delete.contains(t)){
                delete.remove(t);
            }
            else {
                builder.append(t).append("∫");
            }
        }
        shoppingCarMapper.updateShoppingCar(builder.toString(),account);
        return refresh(account);
    }
}
